<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  <title>aqs原理分析 | JAVA</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="AQS">
  
  
  
  
  <meta name="description" content="AQS概述AQS(Abstract Queued Synchronizer) 抽象队列同步器，定义了一套多线程访问共享资源的同步框架，许多同步类实现都依赖与它，常用ReentrantLock/Semaphoe/CountDownLatch 由一个资源状态int state和同步器（FIFO双向阻塞队列组成）  同步器同步器依赖内部的同步队列（FIFO）来完成同步状态的管理，当前线程获取同步状态失败">
<meta name="keywords" content="AQS">
<meta property="og:type" content="article">
<meta property="og:title" content="AQS原理分析">
<meta property="og:url" content="https://jameslin23.gitee.io/2020/12/27/AQS原理分析/index.html">
<meta property="og:site_name" content="JAVA">
<meta property="og:description" content="AQS概述AQS(Abstract Queued Synchronizer) 抽象队列同步器，定义了一套多线程访问共享资源的同步框架，许多同步类实现都依赖与它，常用ReentrantLock/Semaphoe/CountDownLatch 由一个资源状态int state和同步器（FIFO双向阻塞队列组成）  同步器同步器依赖内部的同步队列（FIFO）来完成同步状态的管理，当前线程获取同步状态失败">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227152453788.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227170001772.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227172155980.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227173153293.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227174555960.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227175438751.png">
<meta property="og:updated_time" content="2021-01-09T10:36:52.118Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="AQS原理分析">
<meta name="twitter:description" content="AQS概述AQS(Abstract Queued Synchronizer) 抽象队列同步器，定义了一套多线程访问共享资源的同步框架，许多同步类实现都依赖与它，常用ReentrantLock/Semaphoe/CountDownLatch 由一个资源状态int state和同步器（FIFO双向阻塞队列组成）  同步器同步器依赖内部的同步队列（FIFO）来完成同步状态的管理，当前线程获取同步状态失败">
<meta name="twitter:image" content="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227152453788.png">
  
    <link rel="alternate" href="/atom.xml" title="JAVA" type="application/atom+xml">
  

  

  <link rel="icon" href="/css/images/mylogo.jpg">
  <link rel="apple-touch-icon" href="/css/images/mylogo.jpg">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt; src:url("/css/fonts/FuturaPTBold.otf") format("woff");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt-light; src:url("/css/fonts/FuturaPTBook.otf") format("woff");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt-italic; src:url("/css/fonts/FuturaPTBookOblique.otf") format("woff");font-weight:400;font-style:italic;}
}

  </style>
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>
  <script src="/js/bootstrap.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css">

  
    <link rel="stylesheet" href="/css/dialog.css">
  

  

  
    <link rel="stylesheet" href="/css/header-post.css">
  

  
  
  
    <link rel="stylesheet" href="/css/vdonate.css">
  

</head>
</html>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="border-width: 0;">
                <p>JAVA</p>
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/archives">归档</a> </li>
                
                  <li> <a class="main-nav-link" href="/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/tags">标签</a> </li>
                
                  <li> <a class="main-nav-link" href="/about">关于</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder>
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="请输入关键词...">
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(无标题)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div></li>
            </ul></div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-AQS原理分析" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost">
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" itemprop="name">
      AQS原理分析
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/2020/12/27/AQS原理分析/" class="article-date">
	  <time datetime="2020-12-27T07:17:02.000Z" itemprop="datePublished">2020-12-27</time>
	</a>

      
    <a class="article-category-link" href="/categories/并发编程/">并发编程</a>

      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		阅读量<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

      

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="AQS概述"><a href="#AQS概述" class="headerlink" title="AQS概述"></a>AQS概述</h2><p>AQS(Abstract Queued Synchronizer) 抽象队列同步器，定义了一套多线程访问共享资源的同步框架，许多同步类实现都依赖与它，常用ReentrantLock/Semaphoe/CountDownLatch</p>
<p>由一个资源状态int state和同步器（FIFO双向阻塞队列组成）</p>
<p><img src="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227152453788.png" alt="image-20201227152453788"></p>
<h3 id="同步器"><a href="#同步器" class="headerlink" title="同步器"></a>同步器</h3><p>同步器依赖内部的同步队列（FIFO）来完成同步状态的管理，当前线程获取同步状态失败时，同步器会使用<strong>CAS</strong>将当前线程以及等待状态等信息构造成一个节点Node并将其加入同步队列<strong>尾部</strong>，同时会阻塞当前线程，当同步状态释放时，会把首节点的线程唤醒，并其再尝试获取同步状态。</p>
<p>同步队列的节点Node用来保存获取同步状态失败的线程引用、等待状态以及前驱和后继节点。</p>
<p>同步队列遵循FIFO，首节点是获取同步状态成功的节点，首节点的线程在释放同步状态时，将会唤醒后续节点，而后续节点将会在获取同步状态成功时将自己设置为首节点。</p>
<p><strong>注：由于获取成功状态的线程只有一个，所有设置首节点不需要使用cas，而插入尾部竞争的线程有很多个，所以需要使用CAS。</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227170001772.png" alt="image-20201227170001772"></p>
<h3 id="独占式"><a href="#独占式" class="headerlink" title="独占式"></a>独占式</h3><p>同步器的acquire方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">acquire</span><span class="params">(<span class="keyword">int</span> arg)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(!tryAcquire(arg) &amp;&amp; acquireQueued(addWaiter(Node.EXCLUSIVE),arg))</span><br><span class="line">        selfInterrupt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上述代码主要完成同步锁状态获取、节点构造、加入同步队列以及在同步队列中自旋等待相关工作。</p>
<p>其主要逻辑是：首先调用自定义同步器实现的tryAcquire(int arg)方法，该方法保证线程安全的获取同步状态，如果同步状态获取失败，则构造同步节点并通过addWaiter(Node node)方法将节点加入到同步队列的尾部,最后调用acquireQueued(Node node,int arg)方法，<strong>使得该节点以“死循环”的方式获取同步状态。如果获取不到则阻塞节点的线程，而被阻塞线程的唤醒主要依靠前驱节点的出队或者阻塞线程中断来实现。</strong></p>
<p>当前线程在“死循环”中获取同步状态，而只有前驱节点是头节点才能尝试获取同步状态，这个为什么？</p>
<ul>
<li><strong>头节点是成功获取到同步状态的节点，而头节点的线程释放了同步状态之后，将会唤醒其后继节点，后继节点的线程被唤醒后需要检查自己的前驱节点是否是头节点。</strong></li>
<li><strong>维护同步队列的FIFO原则</strong></li>
</ul>
<p><strong>总结：在获取同步状态时，同步器维护一个同步队列，获取状态失败的线程通过CAS加入到队列尾部并在队列中；移除队列的条件是前驱节点为头节点且成功获取了同步状态。在释放同步状态时，同步器调用tryRelease(int arg)方法释放同步状态。然后唤醒头节点后继节点。</strong></p>
<h3 id="共享式"><a href="#共享式" class="headerlink" title="共享式"></a>共享式</h3><p>共享式获取与独占式获取最主要的区别是在与同一个时刻能有多个线程获取同步状态</p>
<p>在acquireShared(int arg)方法中，同步器调用tryAcquireShared(int arg)方法尝试获取同步状态，tryAcquireShared(int arg)方法返回值为int类型，当返回值大于等于0时，表示能够获取到同步状态。因此，在共享式获取的自旋过程中，成功获取到同步状态并退出自旋的条件就是<strong>tryAcquireShared(int arg)方法返回值大于等于0</strong>。可以看到，在doAcquireShared(int arg)方法的自旋过程中，如果当前节点的前驱为头节点时，尝试获取同步状态，如果返回值大于等于0，表示该次获取同步状态成功并从自旋过程中退出。</p>
<p>与独占式一样，共享式获取也需要释放同步状态，通过调用releaseShared(int arg)方法可以释放同步状态</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">boolean</span> <span class="title">releaseShard</span><span class="params">(<span class="keyword">int</span> arg)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(tryReleaseShard(arg))&#123;</span><br><span class="line">        	doReleaseShared();</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    reture <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该方法在释放同步状态之后，将会唤醒后续处于等待状态的节点。对于能够支持多个线程同时访问的并发组件（比如Semaphore），它和独占式主要区别在于tryReleaseShared(int arg)方法必须确保同步状态（或者资源数）线程安全释放，一般是通过<strong>循环</strong>和<strong>CAS</strong>来保证的，因为释放同步状态的操作会同时来自多个线程。</p>
<h2 id="Condition接口"><a href="#Condition接口" class="headerlink" title="Condition接口"></a>Condition接口</h2><p>任意一个java对象，都拥有一组监视器方法（定义在object上）,主要包括wait()、wait(long timeout)、notify()以及notifyAll()方法，这些方法与Synchronized同步关键字配合,可以实现等待/通知。condition接口也提供了类似Object的监听器方法，与Lock配合可以实现等待/通知模式。</p>
<p>Object的监听器方法与Condition接口的对比</p>
<table>
<thead>
<tr>
<th>对比项</th>
<th>Object</th>
<th>Condition</th>
</tr>
</thead>
<tbody><tr>
<td>前置条件</td>
<td>获取对象锁</td>
<td>调用Lock.lock()获取对象锁，调用Lock.newCondition()获取Condition对象</td>
</tr>
<tr>
<td>调用方式</td>
<td>直接调用</td>
<td>直接调用，例如condition.await()</td>
</tr>
<tr>
<td>等待队列个数</td>
<td>一个</td>
<td>多个</td>
</tr>
<tr>
<td>当前线程释放锁进入等待状态</td>
<td>支持</td>
<td>支持</td>
</tr>
<tr>
<td>当前线程释放锁并进入等待状态，在等待状态中不响应中断</td>
<td>不支持</td>
<td>支持</td>
</tr>
<tr>
<td>当前线程释放锁并进入超时等待状态</td>
<td>支持</td>
<td>支持</td>
</tr>
<tr>
<td>当前线程释放锁并进入等待状态到将来的某个时间</td>
<td>不支持</td>
<td>支持</td>
</tr>
<tr>
<td>唤醒等待队列中一个线程</td>
<td>支持</td>
<td>支持</td>
</tr>
<tr>
<td>唤醒等待队列中全部线程</td>
<td>支持</td>
<td>支持</td>
</tr>
</tbody></table>
<h3 id="实现分析"><a href="#实现分析" class="headerlink" title="实现分析"></a>实现分析</h3><p>ConditionObject是同步器AQS的内部类，因为Condition的操作需要相关联的锁，所以作为同步器的内部类也比较合理。每个condition对象都包含一个队列，该队列是Condition对象实现等待/通知功能的关键。</p>
<p><strong>等待队列</strong></p>
<p>等待队列是一个FIFO的队列，在队列中每个节点都包含一个线程引用，该线程就是在condition对象等待的线程，如果一个线程调用了condition.await()方法，那么该线程将会释放锁，构造成节点加入等待队列并进行等待中。实际上，节点的定义复用了同步器中节点的定义，也就是说，<strong>同步队列和等待队列中节点类型都是同步器静态内部类</strong></p>
<p>一个Condition包含一个等待队列，Condition拥有首节点firstWaiter和尾节点lastWaiter。当前线程调用Condtion.await()方法，将会以当前线程构造节点，并将节点从尾部加入等待队列，等待队列基本构造</p>
<p><img src="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227172155980.png" alt="image-20201227172155980"></p>
<p>Condition拥有首尾节点的引用，而新增节点只需要将原有的尾节点nextWaiter指向它，并且更新尾节点即可。<strong>更新的过程并没有使用CAS保证，原因在于调用await()方法的线程必定获取了锁的线程，也就是该过程是由锁来保证线程安全的。</strong></p>
<p>在Object的监视器模式上，一个对象拥有一个同步队列和等待队列</p>
<p><strong>在Lock拥有一个同步队列和多个等待队列。</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227173153293.png" alt="image-20201227173153293"></p>
<p>Condition的实现是同步器的内部类，因此每个condition实例都能够访问同步器提供的方法，相当于每个condition都拥有所属同步器的引用。</p>
<h3 id="等待"><a href="#等待" class="headerlink" title="等待"></a>等待</h3><p>从队列（同步队列和等待队列）的角度看await()方法，当调用await()方法时，想当于同步队列的首节点（获取锁的节点）移动到Condition的等待队列中。</p>
<p><code>ConditionObject的await方法</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">await</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (Thread.interrupted())</span><br><span class="line">              <span class="keyword">throw</span> <span class="keyword">new</span> InterruptedException();</span><br><span class="line">       <span class="comment">// 当前线程加入等待队列</span></span><br><span class="line">       Node node = addConditionWaiter();</span><br><span class="line">      <span class="comment">// 释放同步状态，也就是释放锁</span></span><br><span class="line">      <span class="keyword">int</span> savedState = fullyRelease(node);</span><br><span class="line">      <span class="keyword">int</span> interruptMode = <span class="number">0</span>;</span><br><span class="line">      <span class="keyword">while</span> (!isOnSyncQueue(node)) &#123;</span><br><span class="line">        LockSupport.park(<span class="keyword">this</span>);</span><br><span class="line">       <span class="keyword">if</span> ((interruptMode = checkInterruptWhileWaiting(node)) != <span class="number">0</span>)</span><br><span class="line">           <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (acquireQueued(node, savedState) &amp;&amp; interruptMode != THROW_IE)</span><br><span class="line">          interruptMode = REINTERRUPT;</span><br><span class="line">    <span class="keyword">if</span> (node.nextWaiter != <span class="keyword">null</span>)</span><br><span class="line">          unlinkCancelledWaiters();</span><br><span class="line">    <span class="keyword">if</span> (interruptMode != <span class="number">0</span>)</span><br><span class="line">    reportInterruptAfterWait(interruptMode);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>调用该方法的线程成功获取了锁的线程，也就是同步队里的首节点，该方法会将当前线程构造节点并加入等到队里中，然后释放同步状态，唤醒同步队列中的后继节点，然后当前线程会进入等待状态</p>
<p>当等待队列中的节点被唤醒，则唤醒节点的线程开始尝试获取同步状态。如果不是通过其他线程调用Condition.signal()方法唤醒，而是对等待线程进行中断，则会抛出InterruptedException。</p>
<p><img src="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227174555960.png" alt="image-20201227174555960"></p>
<h3 id="通知"><a href="#通知" class="headerlink" title="通知"></a>通知</h3><p>调用condition的signal()方法，将会在唤醒等待队列中等待时间最长的节点（首节点），在唤醒之前，将节点移到同步队列。</p>
<p><code>ConditionObject的signal方法</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">signal</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      <span class="keyword">if</span> (!isHeldExclusively())</span><br><span class="line">          <span class="keyword">throw</span> <span class="keyword">new</span> IllegalMonitorStateException();</span><br><span class="line">      Node first = firstWaiter;</span><br><span class="line">       <span class="keyword">if</span> (first != <span class="keyword">null</span>)</span><br><span class="line">       doSignal(first);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>调用该方法的前置条件是当前线程必须获取锁，可以看到signal()进行了isHeldExclusively()检查，也就是当前线程必须获取了锁的线程，接着获取等待队列的首节点，将其移动到同步队列并使用<strong>LockSupport</strong>唤醒节点中的线程。</p>
<p><img src="https://jameslin23.gitee.io/2020/12/27/AQS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/image-20201227175438751.png" alt="image-20201227175438751"></p>
<p>通过调用同步器的enq(Node node)方法，等待队列中节点线程安全地移动到同步队列。当节点移动到同步队列后，当前线程再使用LockSupport唤醒该节点的线程。</p>
<p>被唤醒的线程，将从await()方法中的while循环中退出，进而调用同步器的acquireQueued()方法加入获取同步状态的竞争中。</p>
<p>Condition的signalAll()方法，相当于对等待队列中的每个节点均执行一次signal()方法，效果就是将等待队列中所有节点全部移动到同步队列中，并唤醒每个节点的线程。</p>

      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: '打赏支持', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'https://raw.githubusercontent.com/iTimeTraveler/iTimeTraveler.github.io/site/source/about/donate/images/WeChanQR.png',
  alipayImage: 'https://raw.githubusercontent.com/iTimeTraveler/iTimeTraveler.github.io/site/source/about/donate/images/AliPayQR.jpg'
});
</script>
      
      
      <div>
        <ul class="post-copyright">
          <li class="post-copyright-author">
          <strong>本文作者:  </strong>LeBron Tao
          </li>
          <li class="post-copyright-link">
          <strong>本文链接:  </strong>
          <a href="/2020/12/27/AQS原理分析/" target="_blank" title="AQS原理分析">https://jameslin23.gitee.io/2020/12/27/AQS原理分析/</a>
          </li>
          <li class="post-copyright-license">
            <strong>版权声明:   </strong>
            本博客所有文章除特别声明外，均采用 <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">CC BY-NC-ND 4.0</a>
            许可协议。转载请注明出处
          </li>
         
        </ul>
<div>
</div></div>
      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8yOTQ4MS82MDQ5">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
        
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/AQS/">AQS</a></li></ul>

      

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/12/28/AQS源码分析/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          AQS源码分析
        
      </div>
    </a>
  
  
    <a href="/2020/12/26/ArrayList源码分析/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">ArrayList源码分析</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#AQS概述"><span class="nav-number">1.</span> <span class="nav-text">AQS概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#同步器"><span class="nav-number">1.1.</span> <span class="nav-text">同步器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#独占式"><span class="nav-number">1.2.</span> <span class="nav-text">独占式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#共享式"><span class="nav-number">1.3.</span> <span class="nav-text">共享式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Condition接口"><span class="nav-number">2.</span> <span class="nav-text">Condition接口</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#实现分析"><span class="nav-number">2.1.</span> <span class="nav-text">实现分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#等待"><span class="nav-number">2.2.</span> <span class="nav-text">等待</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#通知"><span class="nav-number">2.3.</span> <span class="nav-text">通知</span></a></li></ol></li></ol>
    
    </div>
  </aside>

</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <p> Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/iTimeTraveler/hexo-theme-hiker" target="_blank">Hexo-theme-hiker</a> </p>
	      <p id="copyRightEn">Copyright &copy; 2013 - 2021 JAVA All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				访客数 : <span id="busuanzi_value_site_uv"></span> |  
				访问量 : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>




  <script src="/js/dialog.js"></script>








	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            JAVA
          </div>
          <div class="panel-body">
            Copyright © 2021 LeBron Tao All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>